// pages/activityDetails/activityDetails.js
var WxParse = require('../../wxParse/wxParse.js');
const QQMapWX = require('../../utils/qqmap-wx.js');
const Format = require("../../utils/time.js");
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        downcount: true,
        activity: { name: "OPG携手USC, UCR, 吃货小分队,UCI和Miibox送你北美华OPG携手USC, UCR, 吃货小分队,UCI和Miibox送你北美华", img: ["../../images/user.png", "../../images/user.png", "../../images/user.png", "../../images/user.png"], djImg: "../../images/user.png", djName: 'Bassjackers', address: "Time Nightclub", city: "Los Angeles", age: " 21+", lable: ["#top100", " #onepartygo", " #may"], sponsorImg: "../../images/user.png", sponsorname: "OPG琪琪", grade: "7", sell: 6, sellImg: ["../../images/user.png", "../../images/user.png", "../../images/user.png", "../../images/user.png"], time: "12月16日 周五 10:30pm-02:30am", djtime: "00:00-02:30", deskP: 1900, ticketP: 10, sponsorTel: "1535645645" },
        examine: true, //当图文详情高度小于380px时，展开活动详情按钮不限
        isChinese: wx.getStorageSync("isChinese"),
        loadMore: true,
        actId: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.setData({
            isChinese: wx.getStorageSync("isChinese"),
            actId: options.id
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "万趴" : "Onepartygo"
        })
        app.getApi.getNsProducts("POST", {
            eventPromoteId: options.id,
            method: "opg.eventPromote.find"
        }).then(data => {
            app.globalData.eventName = data.data.result[0].eventName
            app.globalData.venueName = data.data.result[0].venueName
            app.globalData.cityName = data.data.result[0].cityName
            app.globalData.location = data.data.result[0].location
            var newTime = new Date().getTime()
            var sayTime = data.data.result[0].promoteTime //喊话时间
            var promoteTime
            if ((((sayTime - newTime)) / 86400000 > 7)) { //剩余时间大于一天
                promoteTime = this.data.isChinese ? +"7天前" : "7 days Ago"
            } else if ((((sayTime - newTime)) / 86400000 < 7) && (((sayTime - newTime)) / 86400000 > 2)) {
                promoteTime = this.data.isChinese ? "一周内" : "Week"
            } else if ((((sayTime - newTime)) / 86400000 < 2) && (((sayTime - newTime)) / 86400000 > 1)) {
                promoteTime = this.data.isChinese ? "昨天" : "Yesterday"
            } else {
                promoteTime = this.data.isChinese ? "今天" : "Today"
            }
            that.setData({
                promoteTime: promoteTime
            })
            var startTime = data.data.result[0].startTimeStr;
            var ssdat = new Date(startTime.substring(0, 4), parseInt(startTime.substring(5, 7)) - 1, startTime.substring(8, 10))
            var stateMonth = Format.getMonth(parseInt(startTime.substring(5, 7)), that.data.isChinese),
                stateDay = parseInt(startTime.substring(8, 10)) + (that.data.isChinese ? '日 ' : ' '),
                stateWeek = Format.getWeek(ssdat.getDay(), that.data.isChinese),
                stateHours = (ssdat.getHours() > 12 ? 24 - ssdat.getHours() : ssdat.getHours() == 0 ? 12 : ssdat.getHours()) + (startTime.substring(13, 16) + (ssdat.getHours() >= 12 ? "pm" : "am"));
            data.data.result[0].startTimeStr = stateMonth + stateDay + stateWeek + " " + stateHours
            var endTime = data.data.result[0].endTimeStr;
            var endat = new Date(endTime.substring(0, 4), parseInt(endTime.substring(5, 7)) - 1, endTime.substring(8, 10))
            var endMonth = Format.getMonth(parseInt(endTime.substring(5, 7)), that.data.isChinese),
                endDay = parseInt(endTime.substring(8, 10)) + (that.data.isChinese ? '日 ' : ' '),
                endWeek = Format.getWeek(endat.getDay(), that.data.isChinese),
                endHours = (endat.getHours() > 12 ? 24 - endat.getHours() : endat.getHours() == 0 ? 12 : endat.getHours()) + (endTime.substring(13, 16) + (endat.getHours() >= 12 ? "pm" : "am"));
            data.data.result[0].startTimeStr = stateMonth + stateDay + stateWeek + " " + stateHours
            data.data.result[0].endTimeStr = endMonth + endDay + endWeek + " " + endHours
            app.globalData.activityDetails = data.data.result[0]
            WxParse.wxParse('article', 'html', data.data.result[0].detail, that, 5);

            // 调用接口
            that.setData({
                loadMore: false,
                items: data.data.result[0],
                eventId: data.data.result[0].eventId,
                id: data.data.result[0].id,
                eventType: data.data.result[0].eventType,
                venueName: app.globalData.venueName,
                eventName: app.globalData.eventName,
                cityName: app.globalData.cityName,
                location: data.data.result[0].location
            })
        }).catch(res => {})
    },
    down(e) { //查看dj
        this.setData({
            downcount: !this.data.downcount
        })
    },

    buyTicket(e) {
        var timestamp = Date.parse(new Date());
        var closeTime = parseInt(this.data.items.closeTime)
        if (closeTime - timestamp < 0) {
            wx.showModal({
                content: this.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        return
                    }
                }
            })

        } else {
            wx.navigateTo({
                url: '../buyTicketS/buyTicketS?eventId=' + this.data.eventId + "&id=" + this.data.id + "&eventType=" + this.data.eventType + "&venueName=" + this.data.venueName + "&eventName=" + this.data.eventName + "&cityName=" + this.data.cityName,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        }

    },
    choose(e) { //买桌
        var timestamp = Date.parse(new Date());
        var closeTime = parseInt(this.data.items.closeTime)
        if (closeTime - timestamp < 0) {
            wx.showModal({
                content: this.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                showCancel: false,
                success: function(res) {
                    if (res.confirm) {
                        return
                    }
                }
            })

        } else {
            wx.navigateTo({
                url: '../chooseTables/chooseTables?eventId=' + this.data.eventId + "&id=" + this.data.id + "&eventType=" + this.data.eventType + "&venueName=" + this.data.venueName + "&eventName=" + this.data.eventName + "&cityName=" + this.data.cityName,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        }

    },
    menu(e) { //菜单

        wx.navigateTo({
            url: '../menu/menu',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    queryMultipleNodes: function(that) {
        wx.createSelectorQuery().selectAll('#contentDetail').boundingClientRect(function(rects) {

            rects.forEach(function(rect) {
                if (rect.height > 380) {
                    that.setData({
                        examine: true
                    })
                }

                rect.height // 节点的高度
            })
        }).exec()
    },
    unfold(e) { // 查看活动详情（收起）
        this.setData({
            examine: !this.data.examine
        })
    },
    lookImg(e) {
        let imgList = e.currentTarget.dataset.imglist
        wx.previewImage({
            current: imgList[0], // 当前显示图片的http链接
            urls: imgList // 需要预览的图片http链接列表
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },
    toMap: function() { //进入地图页面
        // let location = this.data.cityName + this.data.location;
        if (this.data.items.countryName == 'China') {
            var location = this.data.cityName + this.data.location;
        } else {
            var location = this.data.location + "," + this.data.cityName;
        }
        var distance = new QQMapWX({
            key: 'VTSBZ-E4ICU-FHMVM-4NCEY-YBYU2-YAFYY' // 必填
        });
        var that = this
        console.log(location)
        if (location != null) {
            distance.geocoder({
                address: location,
                success: function(res) {
                    if (res.status == 0) {
                        wx.openLocation({
                            latitude: parseFloat(res.result.location.lat),
                            longitude: parseFloat(res.result.location.lng),
                            scale: 18,
                            name: res.result.location.title,
                            address: location
                        })
                    } else {
                        wx.showModal({
                            // content: that.data.isChinese ? '无法为您导航到' + that.data.location + "，点击“确定”选择其他地图app" : 'Cannot navigate to' + that.data.location + ", pls click 'confirm' to choose other map app",
                            // // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                            // confirmText: that.data.isChinese ? "确定" : "Confirm",
                            // cancelText: that.data.isChinese ? "取消" : "Cancel",
                            content: that.data.isChinese ? '无法为您导航到' + that.data.location : 'Cannot navigate to' + that.data.location,
                            // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                            confirmText: that.data.isChinese ? "复制地点" : "Copy",

                            success: function(res) {
                                if (res.confirm) {
                                    wx.setClipboardData({
                                        data: location,
                                        success: function(res) {
                                            wx.getClipboardData({
                                                success: function(res) {
                                                    // wx.getLocation({
                                                    //     type: 'gcj02', //返回可以用于wx.openLocation的经纬度
                                                    //     success: function(res) {
                                                    //         var latitude = res.latitude
                                                    //         var longitude = res.longitude
                                                    //         wx.openLocation({
                                                    //             latitude: latitude,
                                                    //             longitude: longitude,
                                                    //             address: location,
                                                    //             scale: 28
                                                    //         })
                                                    //     }
                                                    // })
                                                }
                                            })
                                        }
                                    })
                                }
                            }
                        })
                    }

                },
                fail: function(res) {
                    wx.showModal({
                        // content: that.data.isChinese ? '无法为您导航到' + that.data.location + "，点击“确定”选择其他地图app" : 'Cannot navigate to' + that.data.location + ", pls click 'confirm' to choose other map app",
                        // // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                        // confirmText: that.data.isChinese ? "确定" : "Confirm",
                        // cancelText: that.data.isChinese ? "取消" : "Cancel",
                        content: that.data.isChinese ? '无法为您导航到' + that.data.location : 'Cannot navigate to' + that.data.location,
                        // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                        confirmText: that.data.isChinese ? "复制地点" : "Copy",

                        success: function(res) {
                            if (res.confirm) {
                                wx.setClipboardData({
                                    data: location,
                                    success: function(res) {
                                        wx.getClipboardData({
                                            success: function(res) {
                                                // wx.getLocation({
                                                //     type: 'gcj02', //返回可以用于wx.openLocation的经纬度
                                                //     success: function(res) {
                                                //         var latitude = res.latitude
                                                //         var longitude = res.longitude
                                                //         wx.openLocation({
                                                //             latitude: latitude,
                                                //             longitude: longitude,
                                                //             address: location,
                                                //             scale: 28
                                                //         })
                                                //     }
                                                // })
                                            }
                                        })
                                    }
                                })
                            }
                        }
                    })
                },
                complete: function(res) {}
            });

        } else {

            wx.showModal({
                // content: that.data.isChinese ? '无法为您导航到' + that.data.location + "，点击“确定”选择其他地图app" : 'Cannot navigate to' + that.data.location + ", pls click 'confirm' to choose other map app",
                // // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                // confirmText: that.data.isChinese ? "确定" : "Confirm",
                // cancelText: that.data.isChinese ? "取消" : "Cancel",
                content: that.data.isChinese ? '无法为您导航到' + that.data.location : 'Cannot navigate to' + that.data.location,
                // content: that.data.isChinese ? '线上售票已截止，请客官到活动现场购买！' : "Online ticket sold out，you can purchase at the event site.",
                confirmText: that.data.isChinese ? "复制地点" : "Copy",

                success: function(res) {
                    if (res.confirm) {
                        wx.setClipboardData({
                            data: location,
                            success: function(res) {
                                wx.getClipboardData({
                                    success: function(res) {
                                        // wx.getLocation({
                                        //     type: 'gcj02', //返回可以用于wx.openLocation的经纬度
                                        //     success: function(res) {
                                        //         var latitude = res.latitude
                                        //         var longitude = res.longitude
                                        //         wx.openLocation({
                                        //             latitude: latitude,
                                        //             longitude: longitude,
                                        //             address: location,
                                        //             scale: 28
                                        //         })
                                        //     }
                                        // })
                                    }
                                })
                            }
                        })
                    }
                }
            })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    timestamp: function() {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
        return {
            title: this.data.isChinese ? "万趴" : "Onepartygo",
            path: '/pages/activityDetails/index?id=' + this.data.actId,
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    },
})